<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>${name_china}管理列表</title>
<%@include file="/common/head.jsp" %>

<!-- 时间日期插件 -->
<link href="<%=request.getContextPath()%>/static/plugin/My97DatePicker/skin/default/datepicker.css" rel="stylesheet" >
<script src="<%=request.getContextPath()%>/static/plugin/My97DatePicker/WdatePicker.js"></script>

</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>${name_china}管理</span>
        </div>
        <div id="toolbar" style="float: right"></div>
    </div>
    <div class="page-title-border"></div>
</div>

<div class="search-field">
 <span class="field-cont">
        <span class="field-cont-title">推广位ID</span>
        <input type="text" class="summer-input" id="tgwId"/>
    </span>
    <span class="field-cont">
        <span class="field-cont-title">推广位名称</span>
        <input type="text" class="summer-input" id="tgwName"/>
    </span>
    <span class="field-cont">
        <span class="field-cont-title">云店主ID</span>
        <input type="text" class="summer-input" id="userId"/>
    </span>
    <span class="field-cont">
        <span class="field-cont-title">云店主邀请码</span>
        <input type="text" class="summer-input" id="code"/>
    </span>
    <span class="field-btn-wrapper">
        <a id="search" class="btn btn-primary btn-icon wave distance-right"><i class="icon icon-search"></i></a>
        <a id="reset" class="btn btn-icon wave wave-dark"><i class="icon icon-refresh"></i></a>
    </span>
</div>

<div id="t-wrap">
    <table class="summer-datagrid" id="dataList">
	    <thead>
		    <tr>
		        <th>推广位ID</th>
		        <th>推广位名称</th>
		        <th>创建时间</th>
		        <th>店主ID</th>
		        <th>店主名称</th>
		        <th>操作</th>
		    </tr>
	    </thead>
    </table>
</div>

<script>
	//请求url
	var CONST_URL = {
		LIST: basePath + '${common_url}list',
	   	INIT_ADD: basePath + "${common_url}initAdd",
	   	INIT_EDIT: basePath + "${common_url}initEdit",
	   	INIT_VIEW: basePath + "${common_url}initView",
	   	INIT_IMP: basePath + "${common_url}initImp",
	   	EXP: basePath + "${common_url}exp",
	   	DEL: basePath + "${common_url}del"
	};
	
    var  permission = CommonUtil.getPermission('${name_english}');
    
    $(function () {
        // 新建toobar
		$.funcbar({
			id: 'toolbar',
			type: 'toolbar',
			data: permission,
			permit: ['${name_english}:initImp','${name_english}:initAdd','${name_english}:exp'],
		});
		
        // 查询按钮
        $('#search').on('click', reloadTable);
        
        //重置按钮
        $('#reset').on('click', function () {
        	 //重置文本框的值
        	 $('#tgwName').val('');
        	 $('#tgwId').val('');
        	 reloadTable();
        });
        
        //新建
        $('#toolbar').on('click', 'a[data-permit="${name_english}:initAdd"]', function() {
        	window.location.href = CONST_URL.INIT_ADD; 	  	
		});
        //新建
        $('#toolbar').on('click', 'a[data-permit="${name_english}:initImp"]', function() {
        	window.location.href = CONST_URL.INIT_IMP; 	  	
		});
        
       createTable();
        
        //查看
        $('#t-wrap').on('click', 'a[data-permit="${name_english}:initView"]', function() {
        	var $this = $(this),
        	    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
        	window.location.href = CONST_URL.INIT_VIEW +"&id=" + id;
        });
       
        //编辑
        $('#t-wrap').on('click', 'a[data-permit="${name_english}:initEdit"]', function() {
            var $this = $(this),
                id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
            window.location.href = CONST_URL.INIT_EDIT + '?id=' + id;
        });	
        
        //删除
		$('#t-wrap').on('click', 'a[data-permit="${name_english}:del"]', function() {
			var $this = $(this),
			    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
			$.sdialog({
				type: 'confirm',
				msg: '您确定要删除选中的记录吗?',
				onConfirm: function(result) {
				    if (result) {
				    	$.sajax({
			                url : CONST_URL.DEL + "?id=" + id,
			                onLoadSuccess : function() {
			                	 $.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '删除成功！'    
			                	});
			                	reloadTable();  
			                }
			            });
				    		
				    }	
				}
			});
		});
        
		  //导出 
        $('#toolbar').on('click', 'a[data-permit="${name_english}:exp"]', function() {
        	exp(); 	  	
		});
    });
    
    // 重新加载表格
	function reloadTable() {
		$('#dataList').datagrid('reload',{
            ajax: {
            	param: {
            		"code": $('#code').val(),
            		"userId": $('#userId').val(),
            		"tgwId": $('#tgwId').val(),
            		"tgwName": $('#tgwName').val()
            	}
            }
        });
	}
    
    
	 //导出方法
    function exp() {
        //得到查询条件中的value和创建表单
        
        var name = trimSpace($('#name').val()),
		    category = $('#category').combobox('getValue');
    	    //startTime = trimSpace($('#startTime').val()),
    	    //stopTime = trimSpace($('#stopTime').val());
        
        var form = document.createElement('form');//创建表单
        form.method = 'post';//表单提交方式
        form.action = CONST_URL.EXP;//路径
        document.body.appendChild(form);

        //创建查询条件(input隐藏)
        //全称
        if ( name ) {
            var inputName = document.createElement('input');
            inputName.type = 'hidden';
            inputName.value = name;
            inputName.name = 'name';
            form.appendChild(inputName );
        }
        
        if ( category ) {
            var inputCategory = document.createElement('input');
            inputCategory.type = 'hidden';
            inputCategory.value = category;
            inputCategory.name = 'category';
            form.appendChild(inputCategory);
        }
        		       
        //如果没有任何条件就提示用户
        if ( name || category  ) {
            form.submit();
        } else {
            $.sdialog({
                 type : 'confirm',
                 msg : '未添加导出条件，默认为全部导出，确定吗？',
                 onConfirm : function(result) {
                     if (result) {
                         form.submit();
                     }
                 }
             });
        }
    }
    
    //创建表格
    function createTable() {
    	$('#dataList').datagrid({
    		ajax: {
    			url: CONST_URL.LIST,
    			param: {
    				"code": $('#code').val(),
    				"userId": $('#userId').val(),
    				"tgwId": $('#tgwId').val(),
            		"tgwName": $('#tgwName').val()
            	}
    		},
    		columns: [
    		     {
    		    	 field: 'id',
    		    	 hidden: true
    		     },
    		     {
    		    	 field: 'tgwId'
    		     },
    		     {
    		    	 field: 'tgwName'
    		     },
    		     {
    		    	 field: 'createTime'
    		     },
    		     {
    		    	 field: 'userId'
    		     },
    		     {
    		    	 field: 'userNickName'
    		     },
    		     {
						render : function(rowData) {
							return $.funcbar({
										data : permission, //请求的数据
										type : 'text', //类型
										display : 2, //显示数量
										permit : [
												'${name_english}:initEdit',
												'${name_english}:del'
												 ]
									//需要的权限按钮
									});
						}
					}
    		          
    	    ]
    	});
    }
    
   
   
</script>
</body>
</html>